package com.ctshk.rpc.order.ship.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.ship.dto.ShipOrderTouristDTO;
import com.ctshk.rpc.order.ship.entity.ShipOrder;
import com.ctshk.rpc.order.ship.entity.ShipOrderTourist;
import com.ctshk.rpc.order.ship.mapper.ShipOrderMapper;
import com.ctshk.rpc.order.ship.mapper.ShipOrderTouristMapper;
import com.ctshk.rpc.order.ship.req.ShipOrderTouristModifyReq;
import com.ctshk.rpc.order.ship.req.ShipOrderTouristReq2;
import com.ctshk.rpc.order.ship.service.IShipOrderTouristService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.houbb.heaven.util.lang.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 船票订单旅客信息 服务实现类
 * </p>
 *
 * @author 刘蕾
 * @since 2021-03-16
 */
@DubboService
public class ShipOrderTouristServiceImpl extends ServiceImpl<ShipOrderTouristMapper, ShipOrderTourist> implements IShipOrderTouristService {

    @Autowired
    private ShipOrderTouristMapper shipOrderTouristMapper;
    @Autowired
    private ShipOrderMapper shipOrderMapper;

    @Override
    public Result<List<ShipOrderTouristDTO>> list(ShipOrderTouristReq2 localFunOrderTouristReq, TokenUser currentUser) {
        QueryWrapper<ShipOrderTourist> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("order_id",localFunOrderTouristReq.getOrderId());
        List<ShipOrderTourist> list = shipOrderTouristMapper.selectList(wrapper);
        List<ShipOrderTouristDTO> localFunOrderTouristDTOList = new ArrayList<>();
        if(CollectionUtils.isEmpty(list)) {
            return Result.success(localFunOrderTouristDTOList);
        }
        for(ShipOrderTourist localFunOrderTourist : list){
            ShipOrderTouristDTO shipOrderTouristDTO = EntityUtil.copy(localFunOrderTourist,ShipOrderTouristDTO.class);
            QueryWrapper<ShipOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_deleted",0);
            queryWrapper.eq("id",localFunOrderTourist.getOrderId());
            ShipOrder shipOrder = shipOrderMapper.selectOne(queryWrapper);
            if(shipOrder != null){
                String ticketPrice = localFunOrderTourist.getTicketPrice();
                if(StringUtil.isNotEmpty(ticketPrice)){
                    String[] jsonObjects = ticketPrice.split("/");
                    Map map = JSON.parseObject(jsonObjects[0], Map.class);
                    BigDecimal price = (BigDecimal) map.get("price");
                    BigDecimal tax = (BigDecimal) map.get("tax");
                    shipOrderTouristDTO.setSinglePrice(price);
                    shipOrderTouristDTO.setSingleTaxFee(tax);
                    if(jsonObjects.length > 1){
                        Map map2 = JSON.parseObject(jsonObjects[1], Map.class);
                        BigDecimal price2 = (BigDecimal) map2.get("price");
                        BigDecimal tax2 = (BigDecimal) map.get("tax");
                        shipOrderTouristDTO.setBackPrice(price2);
                        shipOrderTouristDTO.setBackTaxFee(tax2);
                    }
                }

                shipOrderTouristDTO.setPaymentStatus(shipOrder.getPaymentStatus());
                shipOrderTouristDTO.setTripType(shipOrder.getTripType());
            }
            localFunOrderTouristDTOList.add(shipOrderTouristDTO);
        }

        return Result.success(localFunOrderTouristDTOList);
    }

    @Override
    public Result modify(ShipOrderTouristModifyReq shipOrderTouristModifyReq, TokenUser currentUser) {
        ShipOrderTourist shipOrderTourist = shipOrderTouristMapper.selectById(shipOrderTouristModifyReq.getId());
        if(shipOrderTourist != null){
            shipOrderTourist.setSurname(shipOrderTouristModifyReq.getSurname());
            shipOrderTourist.setName(shipOrderTouristModifyReq.getName());
            shipOrderTourist.setTouristName(shipOrderTouristModifyReq.getSurname()+shipOrderTouristModifyReq.getName());
            shipOrderTourist.setAppellation(Integer.valueOf(shipOrderTouristModifyReq.getTouristType()));
            shipOrderTourist.setBirthday(shipOrderTouristModifyReq.getBirthday());
            shipOrderTourist.setCertificatesNumber(shipOrderTouristModifyReq.getCertificatesNumber());
            shipOrderTourist.setVaildDate(DateUtil.toLocalDateTime(shipOrderTouristModifyReq.getVaildDate()));
            shipOrderTourist.setCertificatesTypeId(Long.parseLong(shipOrderTouristModifyReq.getCertificatesType().getValue()));
            shipOrderTourist.setCertificatesType(shipOrderTouristModifyReq.getCertificatesType().getLabel());
            shipOrderTourist.setGmtModified(LocalDateTime.now());
            shipOrderTourist.setModifiedId(currentUser.getId());
            shipOrderTouristMapper.updateById(shipOrderTourist);
        }
        return Result.success(shipOrderTourist.getId());
    }
}
